package com.tju.resource.business.tb_shipment.report;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tju.resource.business.tb_impl_order.report.TbImplReportOrderServiceImpl;
import com.tju.resource.business.tb_shipment.TbShipmentService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.result.SimpleXls;
import com.tju.resource.tool.tool.ClientParameters;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class TbShipReportServiceImpl implements TbShipReportService {

    @Autowired
    TbShipmentService shipService;

    private static final String baseXlsPath = TbImplReportOrderServiceImpl.class.getResource("/res/xlsTemplate/TbShipment").toString();

    @Override
    public boolean wrSimple(JSONObject data, OutputStream out) {
        String file = baseXlsPath + "/HY_Template.xls";
        InputStream inputStream = TbImplReportOrderServiceImpl.class.getResourceAsStream("/res/xlsTemplate/TbShipment/HY_Template.xls");
        boolean exist = FileUtil.exist(file);

        if (!exist && !file.contains("jar:")) {
            return false;
        } else {
            try {
                ClientParameters cParams = new ClientParameters();
                Result<Object> objectResult;
                JSONArray list;
                JSONArray dataList = new JSONArray();
                AtomicInteger box = new AtomicInteger(0);

                cParams.setOne(data.getStr("one"));
                cParams.setTwo(data.getStr("two"));
                cParams.setBeginDate(data.getStr("beginDate"));
                cParams.setFinishDate(data.getStr("finishDate"));
                cParams.setThree(data.getInt("three"));
                cParams.setPage(1);
                cParams.setLimit(999);
                objectResult = shipService.selectShipment(cParams);
                list = JSONUtil.parseArray(objectResult.getData());

                list.forEach(v -> {
                    JSONObject object = JSONUtil.parseObj(v);

                    if (null != object.get("shipMatlList")) {
                        JSONArray matlDataArr = object.getJSONArray("shipMatlList");

                        matlDataArr.forEach(md -> {
                            JSONObject matlData = JSONUtil.parseObj(md);
                            JSONObject d = new JSONObject();

                            d.putOnce("orderNumber", object.getStr("orderNumber"));
                            d.putOnce("matlNo", matlData.getStr("matlNumber"));
                            d.putOnce("matlName", matlData.getStr("matlName"));
                            d.putOnce("color", matlData.getStr("color"));
                            d.putOnce("unit", matlData.getStr("pcsUnit"));
                            d.putOnce("quantity", matlData.getStr("shipQty"));
                            d.putOnce("remark", matlData.getStr("packQty") + "*" + matlData.getStr("boxQty") + "箱" +
                                    (StringUtils.isEmpty(matlData.getStr("lastBox")) || matlData.getInt("lastBox") == 0 ? ""
                                            : "+" + (matlData.getInt("shipQty") - (matlData.getInt("packQty") * matlData.getInt("boxQty")))
                                            + "*" + matlData.getInt("lastBox") + "箱"));

                            box.set(box.get() + matlData.getInt("boxQty"));
                            box.set(box.get() + matlData.getInt("lastBox"));
                            dataList.add(d);
                        });
                    }
                });

                data.remove("list");
                data.putOnce("list", dataList);
//                data.putOnce("no", data.getStr("sn"));
                data.putOnce("totalBox", box.get());

                if (file.indexOf("jar:") == 0) {
                    SimpleXls.fillTemplate(FileUtil.file(file).getAbsolutePath(), inputStream, data, out);
                } else {
                    SimpleXls.fillTemplate(FileUtil.file(file).getAbsolutePath(), data, out);
                }

                return true;
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    out.write(ResultUtil.error(ResultEnum.UNKONW_ERROR, "could down excel file, cause: " + e.toString()).toString().getBytes());
                } catch (Exception e1) {
                    return false;
                }
                return false;
            }
        }
    }
}
